package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.enums.AppointmentStatus;
import com.atguigu.lease.web.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.model.entity.ViewAppointment;


import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Override
    public List<AppointmentItemVo> listAppointment(Long userId) {
        LambdaQueryWrapper<ViewAppointment> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ViewAppointment::getUserId,userId);
        List<ViewAppointment> viewAppointmentList = list(wrapper);
        List<AppointmentItemVo> appointmentItemVoList=new ArrayList<>(viewAppointmentList.size());
        for (ViewAppointment appointment : viewAppointmentList) {
            Long id = appointment.getId();
            Date date = appointment.getAppointmentTime();
            AppointmentStatus status = appointment.getAppointmentStatus();
            List<GraphVo> graphVoList=graphInfoService.queryVoByApartmentId(appointment.getApartmentId());
            String apartmentName = apartmentInfoService.getById(appointment.getApartmentId()).getName();
            appointmentItemVoList.add(new AppointmentItemVo(id,apartmentName,graphVoList,date,status));
        }
        return appointmentItemVoList;
    }

    @Override
    public AppointmentDetailVo getAppointmentDetailVo(Long id) {
        ViewAppointment viewAppointment = getById(id);
        ApartmentItemVo apartmentItemVo = apartmentInfoService.queryVoById(viewAppointment.getApartmentId());
        AppointmentDetailVo vo=new AppointmentDetailVo();
        vo.setApartmentItemVo(apartmentItemVo);
        BeanUtils.copyProperties(viewAppointment,vo);
        return vo;
    }
}




